//
//  FWToolBarView.m
//  BirdLoveSheep
//
//  Created by wwj on 16/4/18.
//  Copyright © 2016年 wwj. All rights reserved.
//

#import "FWToolBarView.h"
#import "FWAddTagViewController.h"

@interface FWToolBarView ()
@property (weak, nonatomic) IBOutlet UIView *centerView;
@property (strong, nonatomic) NSMutableArray *tagLabelArray;
@property (weak, nonatomic) UIButton *addBtn;
@end

@implementation FWToolBarView

+ (instancetype)toolBarView{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
}

- (NSMutableArray *)tagLabelArray{
    if (_tagLabelArray == nil) {
        _tagLabelArray = [NSMutableArray array];
    }
    return _tagLabelArray;
}


- (void)awakeFromNib{
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setImage:[UIImage imageNamed:@"tag_add_icon"] forState:UIControlStateNormal];
    addBtn.width = 55;
    addBtn.height = TAGHeight;
    addBtn.x = 0;
    addBtn.y = 0;
    [addBtn addTarget:self action:@selector(onClickAddButton) forControlEvents:UIControlEventTouchUpInside];
    [self.centerView addSubview:addBtn];
    self.addBtn = addBtn;
    [self addTagWithArray:@[@"吐槽", @"糗事"]];
}

- (void)onClickAddButton{
    FWAddTagViewController *addTagVC = [[FWAddTagViewController alloc] init];
    addTagVC.tagsArray = [self.tagLabelArray valueForKeyPath:@"text"];
    UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    UINavigationController *navigationVC = (UINavigationController *)rootVC.presentedViewController;
    [navigationVC pushViewController:addTagVC animated:YES];
    __weak typeof(self) weakSelf = self;
    addTagVC.tagsBlock = ^(NSArray *tagsArray){
        [weakSelf addTagWithArray:tagsArray];
    };
}

- (void)addTagWithArray:(NSArray *)tagsArray{
    [self.tagLabelArray makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.tagLabelArray removeAllObjects];
    
    for (NSInteger index = 0; index < tagsArray.count; index++) {
        UILabel *tagLabel = [[UILabel alloc] init];
        tagLabel.layer.cornerRadius = 4;
        tagLabel.text = tagsArray[index];
        tagLabel.textColor = [UIColor whiteColor];
        tagLabel.backgroundColor = MyColor(97, 173, 232);
        tagLabel.textAlignment = NSTextAlignmentCenter;
        tagLabel.layer.cornerRadius = 4;
        tagLabel.layer.masksToBounds = YES;
        tagLabel.font = [UIFont systemFontOfSize:15];
        [tagLabel sizeToFit];
        tagLabel.height = TAGHeight;
        tagLabel.width += TAGMARGIN;
        [self.centerView addSubview:tagLabel];
        [self.tagLabelArray addObject:tagLabel];
    }
    
    [self setNeedsLayout];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    for (NSInteger index = 0; index < self.tagLabelArray.count; index++) {
        UILabel *tagLabel = self.tagLabelArray[index];
        if (index == 0) {
            tagLabel.x = 0;
            tagLabel.y = 0;
        }else {
            UILabel *lastLabel = self.tagLabelArray[index - 1];
            CGFloat leftWith = CGRectGetMaxX(lastLabel.frame) + TAGPADING;
            if (self.centerView.width - leftWith > tagLabel.width) {
                tagLabel.x = leftWith;
                tagLabel.y = lastLabel.y;
            }else{
                tagLabel.x = 0;
                tagLabel.y = CGRectGetMaxY(lastLabel.frame) + TAGPADING;
            }
        }
    }
    
    UILabel *lastLabel = self.tagLabelArray.lastObject;
   
    CGFloat leftWith = CGRectGetMaxX(lastLabel.frame) + TAGPADING;
    if (self.centerView.width - leftWith > self.addBtn.width) {
        self.addBtn.x = leftWith;
        self.addBtn.y = lastLabel.y;
    }else {
        self.addBtn.x = 0;
        self.addBtn.y = CGRectGetMaxY(lastLabel.frame) + TAGPADING;
    }
    CGFloat oldH = self.height;
    self.height = CGRectGetMaxY(self.addBtn.frame) + 44 + TAGHeight;
    self.y -= self.height - oldH;
}

@end
